﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreEx.Common.Proxy;
using CoreEx.Services;
using LinFu.IoC.Interfaces;
using LinFu.IoC;
using LinFu.Proxy.Interfaces;

namespace CoreEx.Database.Persistence
{
    public static class ListExtensions
    {
        private static IServiceContainer _serviceContainer;
        
        static ListExtensions()
        {
            _serviceContainer = ServiceLocator.Container;
        }

        /// <summary>
        /// Returns a proxy implementation of <see cref="IList{T}"/>.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="targetList"></param>
        /// <param name="changeTracker"></param>
        /// <returns></returns>
        public static IList<T> ToTrackableList<T>(this IList<T> targetList, IChangeTracker changeTracker)
        {
            var proxyFactory = _serviceContainer.GetService<IProxyFactory>();
            var proxy = proxyFactory.CreateProxy<IList<T>>(new ListInvokeWrapper(targetList, changeTracker));
            return proxy;
        }
    }
}
